/***************************************************************************************************
 *
 * Project: ChinesePod
 *
 * Class: ExtractRadicals
 *
 * File name: ExtractRadicals.java
 *
 **************************************************************************************************/

package com.eastmountain.chinese.util;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.JPanel;

import com.eastmountain.util.DisplayFormats;

//   file:///C:/Documents%20and%20Settings/All%20Users/Documents/Languages/Chinese/Radicals/radicals.html

public class ExtractRadicals extends JPanel {

    public ExtractRadicals() {

    }

    private void extractRadicalsFromFile() {

        String line = "didn't get a file.";
        String trimmedLine;
        String radicalNumber;
        String radical;

        try {

            FileInputStream fileInputStream = new FileInputStream( "rads1.html" );
            InputStreamReader inputStreamReader = new InputStreamReader( fileInputStream, "UTF-8" );
            BufferedReader bufferedReader = new BufferedReader( inputStreamReader );

            FileOutputStream fileOutputStream = new FileOutputStream( "radical-data.html" );
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter( fileOutputStream, "UTF-8" );


            while ( ( line = bufferedReader.readLine() ) != null ) {

                trimmedLine = line.trim();

                if ( ! "".equals( trimmedLine ) ) {

                    if ( trimmedLine.matches(  "\\d+" ) ) {

                        radicalNumber = trimmedLine;

                        if ( ( line = bufferedReader.readLine() ) != null ) {

                            trimmedLine = line.trim();

                            radical = trimmedLine.substring( 0, 1 );

                            String unicode = DisplayFormats.charToUnicodeString( radical.charAt(  0 ) );

                            String outputLine = String.format( "%3s  %s  %s\n", radicalNumber, radical, unicode );

                            System.out.printf( outputLine );

                            String outputRecord
                                = String.format( "/* %3s */  \"\\u%s\",", radicalNumber, unicode );

                            outputStreamWriter.write( outputRecord + "\n" );

                        }

                    }

                }

            }

            bufferedReader.close();
            outputStreamWriter.close();

        } catch ( FileNotFoundException fnfe ) {

            System.out.println( "Cannot locate input file! " + fnfe.getMessage() );
            System.exit( 12 );

        } catch ( IOException exception ) {

            System.out.println( "Unexpected Exception:  " + exception.getMessage() );
            System.exit( 12 );

        }

    }


    /***********************************************************************************************
     *
     *  main
     *
     **********************************************************************************************/

    public static void main( String argv[] ) {

            ExtractRadicals extractRadicals = new ExtractRadicals();

            extractRadicals.extractRadicalsFromFile();

    } // main

}
